package design.simdi.code.final_paper.site.panels.licente.domenii;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.hibernate.criterion.Criterion;

import design.simdi.code.final_paper.core.daos.DomainDao;
import design.simdi.code.final_paper.core.entities.Domain;
import design.simdi.code.final_paper.core.entities.Person;
import design.simdi.code.final_paper.core.entities.Sesiune;

/**
 * 
 * @author Marius Dumitriu - (Jun 18, 2010 - 11:38:53 AM)
 * 
 */
public class SortableDomainDataProvider extends SortableDataProvider<Domain> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SpringBean
	private DomainDao domainDao;
	private Person owner;
	private Sesiune sesiune;

	private List<Criterion> criterii;

	/**
	 * constructor
	 */
	public SortableDomainDataProvider() {
		InjectorHolder.getInjector().inject(this);
		// set default sort
		setSort("domain.name", true);
		criterii = new ArrayList<Criterion>();
	}

	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#iterator(int,
	 *      int)
	 */
	public Iterator<Domain> iterator(int first, int count) {
		SortParam sp = getSort();
		// Order o;
		// if (sp.isAscending())
		// o = Order.asc(sp.getProperty());
		// else
		// o = Order.desc(sp.getProperty());

		// return domainDao.findAll(new Order[] { o }, criterii).subList(first,
		// first + count).iterator();
		String sortQuery;
		sortQuery = " order by ";

		if (sp.getProperty().equals("name"))
			sortQuery += "domain." + sp.getProperty();
		else if (sp.getProperty().equals("author")) {
			if (sp.isAscending())
				sortQuery += "author.prenume asc" + ",  " + "author.nume";
			else
				sortQuery += "author.prenume desc" + ",  " + "author.nume";
		} else
			sortQuery += sp.getProperty();
		// Order o;
		if (sp.isAscending())
			sortQuery += " asc";
		else
			sortQuery += " desc";
		//
		String query = "";
		if (owner != null)
			query = String
					.format(
							"select distinct domain from Domain as domain, Person as author, Sesiune as sesiune where "
									+ "domain.author = author  and author.id = '%s' and domain.sesiune = sesiune and sesiune.id= '%s'  "
									+ sortQuery, getOwner().getId(), getSesiune().getId());
		else
			query = String.format(
					"select distinct domain from Domain as domain, Person as author, Sesiune as sesiune where "
							+ "domain.author = author and domain.sesiune = sesiune and sesiune.id= '%s' " + sortQuery,
					getSesiune().getId());
		List<Domain> lista = domainDao.find(query);

		return lista.subList(first, first + count).iterator();

	}

	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#size()
	 */
	public int size() {
		// return domainDao.findAll(new Order[] {}, criterii).size();
		SortParam sp = getSort();
		String sortQuery;
		sortQuery = " order by ";

		if (sp.getProperty().equals("name"))
			sortQuery += "domain." + sp.getProperty();
		else
			sortQuery += sp.getProperty();
		// Order o;
		if (sp.isAscending())
			sortQuery += " asc";
		else
			sortQuery += " desc";
		//
		String query = "";
		if (owner != null)
			query = String
					.format(
							"select distinct domain from Domain as domain, Person as author, Sesiune as sesiune where "
									+ "domain.author = author  and author.id = '%s' and domain.sesiune = sesiune and sesiune.id= '%s'  "
									+ sortQuery, getOwner().getId(), getSesiune().getId());
		else
			query = String.format(
					"select distinct domain from Domain as domain, Person as author, Sesiune as sesiune where "
							+ "domain.author = author and  domain.sesiune = sesiune and sesiune.id= '%s' " + sortQuery,
					getSesiune().getId());
		List<Domain> lista = domainDao.find(query);

		return lista.size();
	}

	/**
	 * @see org.apache.wicket.markup.repeater.data.IDataProvider#model(java.lang.Object)
	 */
	public IModel<Domain> model(Domain object) {
		return new DetachableDomainModel(object, domainDao);
	}

	public void setDomainDao(DomainDao domainDao) {
		this.domainDao = domainDao;
	}

	public DomainDao getDomainDao() {
		return domainDao;
	}

	public void setCriterii(List<Criterion> criterii) {
		this.criterii = criterii;
	}

	public List<Criterion> getCriterii() {
		return criterii;
	}

	public void setOwner(Person owner) {
		this.owner = owner;
	}

	public Person getOwner() {
		return owner;
	}

	public void setSesiune(Sesiune sesiune) {
		this.sesiune = sesiune;
	}

	public Sesiune getSesiune() {
		return sesiune;
	}

}